// Need these to get definitions of common interfaces,
// and various connection objects.
using System;
using System.Data;
using System.Data.SqlClient;
using System.Data.Odbc;
using System.Data.OleDb;

// Need to reference System.Data.OracleClient.dll to nab this namespace!
using System.Data.OracleClient;

// Need to also reference System.Configuration.dll.
using System.Configuration;

namespace MyConnectionFactory
{
  // A list of possible providers.
  enum DataProvider
  { SqlServer, OleDb, Odbc, Oracle, None }

  class Program
  {
    static void Main(string[] args)
    {
      Console.WriteLine("**** Very Simple Connection Factory *****\n");

      // Read the provider key.
      string dataProvString = ConfigurationManager.AppSettings["provider"];

      // Transform string to enum.
      DataProvider dp = DataProvider.None;
      if (Enum.IsDefined(typeof(DataProvider), dataProvString))
        dp = (DataProvider)Enum.Parse(typeof(DataProvider), dataProvString);
      else
        Console.WriteLine("Sorry, no provider exists!");

      // Get a specific connection.
      IDbConnection myCn = GetConnection(dp);
      if (myCn != null)
        Console.WriteLine("Your connection is a {0}", myCn.GetType().Name);

      // Open, use and close connection.

      Console.ReadLine();
    }

    #region GetConnection() helper method
    // This method returns a specific connection object
    // based on the value of a DataProvider enum.
    static IDbConnection GetConnection(DataProvider dp)
    {
      IDbConnection conn = null;
      switch (dp)
      {
        case DataProvider.SqlServer:
          conn = new SqlConnection();
          break;
        case DataProvider.OleDb:
          conn = new OleDbConnection();
          break;
        case DataProvider.Odbc:
          conn = new OdbcConnection();
          break;
        case DataProvider.Oracle:
          conn = new OracleConnection();
          break;
      }
      return conn;
    }
    #endregion
  }
}
